package com.littlepay.kr4.paulin.dfeba77.remind;

/**
 * Created by john on 2017/6/8.
 */


import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TimePicker;

import com.littlepay.kr4.paulin.dfeba77.R;
import com.littlepay.kr4.paulin.dfeba77.SettingActivity;

import java.util.Calendar;

/**
 * trigger the Broadcast event and set the alarm
 */
public class ReminderSetting extends Activity {

    Button btnEnable;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder);

        /* create a button. When you click the button, the alarm clock is enabled */
//        btnEnable=(Button)findViewById(R.id.btnenable);
//        btnEnable.setOnClickListener(new View.OnClickListener() {
//            @Override
//           public void onClick(View v) {
//               //setReminder(true);
//                Intent intent = new Intent(ReminderSetting.this,SettingActivity.class);
//                startActivity(intent);
//           }
//        });
        Calendar currentTime = Calendar.getInstance();
        //创建一个TimePickerDialog实例，并显示
        new TimePickerDialog(ReminderSetting.this, 0,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        //指定启动AlarmActivity组件
                        //Intent intent = new Intent(ReminderSetting.this, MyReceiver.class);
                        //创建PendingIntent对象
								/*
								 * PendingIntent与Intent的区别是PendingIntent处理即将发生的事情
								 * 比如：在通知栏Notification中跳转页面，不是立即跳转
								 * 通常通过  getActivity、getBroadcast、getService得到PendingIntent的实例
								 *
								 */
                        //PendingIntent pi = PendingIntent.getActivity(ReminderSetting.this, 0, intent, 0);
                        Calendar c = Calendar.getInstance();
                        c.setTimeInMillis(System.currentTimeMillis());
                        c.set(Calendar.HOUR, hourOfDay);
                        c.set(Calendar.MINUTE, minute);
                        //AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);

                        //am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
                        setReminder(true,c);
                        //Toast.makeText(ReminderSetting.this, "设置提醒成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(ReminderSetting.this,SettingActivity.class);
                        startActivity(intent);
                }
                }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show();
    }

    /**
     * Set the alarm
     *
     * @param b whether enable the Alarm clock or not
     */
    private void setReminder(boolean b,Calendar c) {

        // get the AlarmManager instance
        AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
        // create a PendingIntent that will perform a broadcast
        PendingIntent pi= PendingIntent.getBroadcast(ReminderSetting.this, 0, new Intent(this,MyReceiver.class), 0);

        if(b){
            // just use current time as the Alarm time.
            //Calendar c=Calendar.getInstance();
            // schedule an alarm
            //am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
            am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);

        }
        else{
            // cancel current alarm
            am.cancel(pi);
        }

    }
}